package com.vehicle.controller;
import com.vehicle.service.CollegeDetailService;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/college-detail")
public class CollegeDetailController {

    @Autowired
    CollegeDetailService collegeDetailService;

    //添加学院详情
    @PostMapping("/add/{collegeId}/{detail}")
    public boolean addDetail(HttpServletRequest request, @PathVariable("collegeId") Integer collegeId, @PathVariable("detail") String detail){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeDetailService.addCollegeDetail(ip,Integer.parseInt(id),collegeId, detail);
    }


    //根据cdId获取college_id
    @GetMapping("/collegeId/{cdId}")
    public Integer getCollageIdById(@PathVariable("cdId") Integer cdId) {
        return collegeDetailService.getCollegeIdById(cdId);
    }

    //直接获取所有信息
    @GetMapping("/allList/{page}/{size}")
    public ResultVO getAllList(@PathVariable("page") String page, @PathVariable("size") String size){
        return ResultVO.succ(collegeDetailService.getAllList(Integer.parseInt(page),Integer.parseInt(size)));
    }


    //根据collegeId获取列表
    @GetMapping("/getListByCollegeId/{collegeId}/{page}/{size}")
    public ResultVO getListByCollegeId(@PathVariable("collegeId") Integer collegeId,@PathVariable("page") String page,@PathVariable("size") String size){
        return ResultVO.succ(collegeDetailService.getListByCollegeId(collegeId,Integer.parseInt(page),Integer.parseInt(size)));
    }

    //根据cdId获取detail
    @GetMapping("/getDetail/{cdId}")
    public String getDetailById(@PathVariable("cdId") Integer cdId) {
        return collegeDetailService.getDetailById(cdId);
    }

    //修改学院信息
    @PutMapping("/update/{cdId}/{collegeId}/{detail}")
    public Boolean updateCollage(HttpServletRequest request,@PathVariable("cdId") Integer cdId,@PathVariable("collegeId") Integer collegeId,@PathVariable("detail") String detail){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeDetailService.updateCollegeDetailById(ip,Integer.parseInt(id),cdId, collegeId, detail);
    }

    //删除学院详情
    @DeleteMapping("/delete/{cdId}")
    public Boolean deleteCollege(HttpServletRequest request,@PathVariable("cdId") Integer cdId) {
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeDetailService.deleteCollegeDetail(ip,Integer.parseInt(id),cdId);
    }


}

